﻿using Diablo3.Services.BattleNet;
using Diablo3.Services.Model.Profiles;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;

namespace Diablo3.Services
{
    /// <summary>
    /// 
    /// </summary>
    public class Diablo3ServiceClient : ClientBase<IDiablo3Service>, IDiablo3Service
    {
        #region Ctor

        /// <summary>
        /// Initializes a new instance of the <see cref="Diablo3ServiceClient" /> class.
        /// </summary>
        /// <param name="region">The region.</param>
        /// <param name="localization">The localization.</param>
        public Diablo3ServiceClient()
            : base(new WebHttpBinding(), GetEnpointAddressByRegion(Region.US))
        {
            this.Endpoint.EndpointBehaviors.Add(new WebHttpBehavior());

            ((WebHttpBinding)this.Endpoint.Binding).MaxReceivedMessageSize = 524288;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Gets the base URI by region.
        /// </summary>
        /// <param name="region">The region.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        private static EndpointAddress GetEnpointAddressByRegion(Region region)
        {
            switch (region)
            {
                case Region.US:
                    {
                        return new EndpointAddress("http://us.battle.net/");
                    }
                case Region.EU:
                    {
                        return new EndpointAddress("http://eu.battle.net/");
                    }
                case Region.KR:
                    {
                        return new EndpointAddress("http://kr.battle.net/");
                    }
                case Region.TW:
                    {
                        return new EndpointAddress("http://tw.battle.net/");
                    }
                case Region.CN:
                    {
                        return new EndpointAddress("http://www.battlenet.com.cn/");
                    }
                default:
                    {
                        throw new NotImplementedException();
                    }
            }
        }

        #endregion

        #region IDiablo3Service Implementation

        /// <summary>
        /// Gets the profile.
        /// </summary>
        /// <param name="battleTag">The battle tag.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public Profile GetProfile(BattleTag battleTag)
        {
            this.Endpoint.Address = GetEnpointAddressByRegion(battleTag.Region);

            return this.GetProfile(battleTag.Name, battleTag.Code.ToString(), battleTag.Culture.IetfLanguageTag);
        }

        /// <summary>
        /// Gets the profile.
        /// </summary>
        /// <param name="battletagName">Name of the battletag.</param>
        /// <param name="battletagCode">The battletag code.</param>
        /// <param name="localization">The localization.</param>
        /// <returns></returns>
        public Profile GetProfile(string battletagName, string battletagCode, string localization)
        {
            return base.Channel.GetProfile(battletagName, battletagCode, localization);
        }

        /// <summary>
        /// Gets the profile async.
        /// </summary>
        /// <param name="battleTag">The battle tag.</param>
        /// <returns></returns>
        public Task<Profile> GetProfileAsync(BattleTag battleTag)
        {
            this.Endpoint.Address = GetEnpointAddressByRegion(battleTag.Region);

            return this.GetProfileAsync(battleTag.Name, battleTag.Code.ToString(), battleTag.Culture.IetfLanguageTag);
        }

        /// <summary>
        /// Gets the profile async.
        /// </summary>
        /// <param name="battletagName">Name of the battletag.</param>
        /// <param name="battletagCode">The battletag code.</param>
        /// <param name="localization">The localization.</param>
        /// <returns></returns>
        public Task<Profile> GetProfileAsync(string battletagName, string battletagCode, string localization)
        {
            return base.Channel.GetProfileAsync(battletagName, battletagCode, localization);
        }
        
        #endregion
    }
}
